package ovm.management;

import org.vaadin.addons.lazyquerycontainer.EntityContainer;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.lang.reflect.Method;
import java.util.List;

public class AbstractManager {

    public static final String PersistenceUnit = "NewPersistenceUnit";
    public static EntityManagerFactory emf = Persistence.createEntityManagerFactory(PersistenceUnit);
    public static EntityManager em = emf.createEntityManager();
    public static int SUCCESSFUL = 0;
    public static int UNSUCCESSFUL = -1;

    public static EntityContainer getDefaultEntityContainer(Class clazz) {
        return new EntityContainer(em, true, true, true, clazz, 100,
                new Object[] { "id" }, new boolean[] { true });    
    }
    
    public static List getEntityListByClass (Class clazz) {
        return em.createQuery("SELECT entity FROM "+clazz.getSimpleName()+" entity").getResultList();
    }
    
    public static Object getAbstractEntityById (Class clazz, Integer id) {
        Query query = em.createQuery("SELECT entity FROM "+clazz.getSimpleName()+" entity WHERE entity.id = :id");
        query.setParameter("id", id);
        return query.getResultList().get(0);
    }
    
    public static Class getPropertyClass(Class entityClass, String propertyName) {
        try {
            return entityClass.getDeclaredField(propertyName).getType();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return null;
        }
    }

    public static int persistAbstractEntity (Object entity) {
        try {
            em.getTransaction().begin();
            em.persist(entity);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em = emf.createEntityManager();
            return UNSUCCESSFUL;
        }
        return SUCCESSFUL;
    }
    
    public static int addDefaultCatalogEntity (Class clazz) {
        try {
            Object entity = clazz.newInstance();
            Method method = clazz.getMethod("setValue", String.class);
            method.invoke(entity, "Значение");
            em.getTransaction().begin();
            em.persist(entity);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            em = emf.createEntityManager();
            return UNSUCCESSFUL;
        }
        return SUCCESSFUL;
    }
    
    public static int addDefaultEntity (Class clazz) {
        try {
            Object entity = clazz.newInstance();
            em.getTransaction().begin();
            em.persist(entity);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em = emf.createEntityManager();
            return UNSUCCESSFUL;
        }
        return SUCCESSFUL;
    }
    
     public static int deleteAbstractEntity (Object entity) {
         try {
             em.getTransaction().begin();
             em.remove(entity);
             em.getTransaction().commit();
         } catch (Exception e) {
             e.printStackTrace();
             em = emf.createEntityManager();
             return UNSUCCESSFUL;
         }
         return SUCCESSFUL;
     }
          
}
